本文主要是介绍eureka注册中心上的服务消费者访问服务提供者(重要!!!!!),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
原理:
其中涉及到3中角色:
服务注册中心:作为eureka的一个server,提供服务注册和发现的功能。在启动类中加注解@EnableEurekaServer
服务提供者:作为eureka的一个客户端组件,可以对外提供服务。在启动类中加注解@EnableEurekaClient
服务消费者:作为eureka的一个客户端组件,用来消费服务。在启动类中加注解@EnableEurekaClient
过程:
1.服务注册中心eureka server先启动
2.eureka client的服务提供者向服务注册中心eureka server进行注册。
3.eureka client的服务消费者向服务注册中心eureka server注册,同时从注册中心获取一份服务注册列表的信息,该列表包含了所有向服务注册中心eureka server注册的服务信息。获取服务注册列表信息之后,服务消费者就知道服务提供者的IP地址,可以通过HTTP远程调度来访问提供者提供的服务。
4.服务消费者和服务提供者对于服务注册中心来讲,都是客户端。
consumer:
controller:
package com.ljf.weifuwu.springcloud.eureka.consumer.controller;import com.ljf.weifuwu.springcloud.eureka.consumer.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;@RestController
public class ConsumerUserController
{ @Autowired
private RestTemplate restTemplate;@GetMapping("/consumer-eureka/{id}")public User findById(@PathVariable Long id) {return this.restTemplate.getForObject("http://localhost:7900/eureka-provider/" + id, User.class);//为何不能用下面这种方式??????????//return this.restTemplate.getForObject("http://ms-eureka-provider/eureka-provider/" + id, User.class);}
}
启动类:
package com.ljf.weifuwu.springcloud.eureka.consumer;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;/*** Hello world!**/
@SpringBootApplication
@EnableEurekaClient
public class ConsumerEurekaApp
{ @Bean
public RestTemplate restTemplate() {return new RestTemplate();
}public static void main( String[] args ){SpringApplication.run(ConsumerEurekaApp.class, args);System.out.println( "eureka-consumer启动起来了!!!" );}
}
provider:
controller:
package com.ljf.weifuwu.springcloud.eureka.provider.controller;import com.ljf.weifuwu.springcloud.eureka.provider.model.EurekaUser;import com.ljf.weifuwu.springcloud.eureka.provider.respository.EurekaUserRespository;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;@RestController
public class UserController {@Autowired@Qualifier("eurekaClient")private EurekaClient eurekaClient;@Autowiredprivate DiscoveryClient discoveryClient;@Autowiredprivate EurekaUserRespository userRepository;@GetMapping("/eureka-instance")public String serviceUrl() {//和配置文件配置的application.name=ms-eureka-provider保持一致//获取配置文件的ip和端口号:http://localhost:7900/eureka-instanceInstanceInfo instance = this.eurekaClient.getNextServerFromEureka("ms-eureka-provider", false);return instance.getHomePageUrl();}@GetMapping("/instance-info")public ServiceInstance showInfo() {//http://localhost:7900/instance-info//"host":"192.168.1.225","port":7900,"serviceId":"ms-eureka-provider","metadata":{},"secure":false,"uri":"http://192.168.1.225:7900"}ServiceInstance localServiceInstance = this.discoveryClient.getLocalServiceInstance();return localServiceInstance;}@GetMapping("/eureka-provider/{id}")public EurekaUser getSingleUser(@PathVariable Long id){return this.userRepository.findOne(id);}
}
respository:
package com.ljf.weifuwu.springcloud.eureka.provider.respository;import com.ljf.weifuwu.springcloud.eureka.provider.model.EurekaUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface EurekaUserRespository extends JpaRepository<EurekaUser, Long> {}data.sql:
insert into eurekauser(id,username, name, age, balance) values(1,'user1', 'liu', 20, 100.00);
insert into eurekauser(id,username, name, age, balance) values(2,'user2', 'jian', 20, 100.00);
insert into eurekauser(id,username, name, age, balance) values(3,'user3', 'fu', 20, 100.00);
insert into eurekauser(id,username, name, age, balance) values(4,'user4', 'jurf', 20, 100.00);
schema.sql:
drop table eurekauser if exists;
create table eurekauser(id bigint generated by default as identity,username varchar(40),name varchar(20),age int(3),balance decimal(10,2), primary key(id)
);
启动类:
package com.ljf.weifuwu.springcloud.eureka.provider;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;/*** Hello world!**/
@SpringBootApplication
@EnableEurekaClient
public class EurekaProviderApp
{public static void main( String[] args ){SpringApplication.run(EurekaProviderApp.class,args);System.out.println( "eurka中的提供者启动成功!" );}
}
页面访问:http://localhost:7901/consumer-eureka/1
这篇关于eureka注册中心上的服务消费者访问服务提供者(重要!!!!!)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!