微服务入门案例

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java语录精选

你的鼓励是我坚持下去的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值