Spring Boot 2.2.0
依赖Spring web与EureKa Server
maven项目里创建eureka名Spring Boot 项目
@EnableEurekaServer//开启注册中心服务
@SpringBootApplication
@EnableEurekaServer//开启注册中心服务
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);}}
application.properties
##注册中心服务名字
spring.application.name=eureka
server.port=1111
#是否将自己注册到EureKa中,默认true
eureka.client.register-with-eureka=false
#是否Eurka中心服务中获取注册信息默认true
eureka.client.fotch-registry=false
#注册中心把自己服务信息注册到上面
eureka.client.service-url.defaultZone=http://localhost:1111/eureka
启动http://localhost:1111/看到自己的服务信息
服务端
maven项目里创建名providerSpring Boot 项目
依赖是Spring web与Eureka Discovery Client
application.properties
#服务端名字
spring.application.name=provider
#服务端信息名字和端口注册到注册中心
eureka.client.service-url.defaultZone=http://localhost:1111/eureka
server.port=2000
启动二个服务:http://localhost:1111/服务提供者注册到注册中心
@RestController
public class HelloController {
@Value("${server.port}")
Integer port; //获取端口
@GetMapping("/hello")
public String hello(){
return "服务端hello注册信息"+port;
}
客服端
maven项目里创建名consumerSpring Boot 项目
依赖是Spring web与Eureka Discovery Client
application.properties
#客服端
spring.application.name=consumer
server.port=3000
#客服端服务注册到注册中心
eureka.client.service-url.defaultZone=http://localhost:1111/eureka
UseHelloController消费信息
@RestController
public class UseHelloController {
@Autowired
DiscoveryClient discoveryClient; //发现客服端
@GetMapping("/hello")
public void hello()throws IOException {
List<ServiceInstance> list = discoveryClient.getInstances("provider");//客服端根据服务端名到注册中心列表获取详细信息
ServiceInstance serviceInstance = list.get(0); //链表中根据size取服务实例
String url = "http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/hello";//服务完整信息
HttpURLConnection con = null; //初始化
URL url1 = new URL(url); //服务端http://localhost:2000/hello
con = (HttpURLConnection)url1.openConnection(); //打开服务连接
if (con.getResponseCode() == 200){ //获取响应码200比较
BufferedReader buff = new BufferedReader(new InputStreamReader(con.getInputStream()));//字节流输入到缓存
String s = buff.readLine(); //缓存中读到字符串信息(服务端hello注册2000)
System.out.println(s); //打印(服务端hello注册2000)
buff.close();
}
三个服务启动注册到注册中心http://localhost:1111/
启动客服端http://localhost:3000/hello
总体来说与之前那个http调用相似
客服端负载均衡实现
Buid打包一个jar包启动
java -jar provider-0.0.1-SNAPSHOT.jar --server.post=2001
启动二个提供者服务端
Integer count=0;
ServiceInstance serviceInstance = list.get((count++)%list.size()); //取余是0.1
客服端访问http://localhost:3000/hello
服务端hello注册2001
服务端hello注册2000
服务端hello注册2001
基于RestTemplate实现客服端消费(第二种)
@Bean
RestTemplate restTemplate(){
return new RestTemplate();
}
@Autowired
RestTemplate restTemplate;
@GetMapping("/hello2")
public void hello2()throws IOException {
List<ServiceInstance> list = discoveryClient.getInstances("provider");
ServiceInstance serviceInstance = list.get((count++) % list.size()); //长度2 count 1或0
String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/hello";
String s = restTemplate.getForObject(url,String.class);
System.out.println(s);
}
}
访问http://localhost:3000/hello2