【汇智学堂】微服务-SpringCloud(Eureka+Ribbon)之二

在这里插入图片描述
New module:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
application.yml:

server:
  port: 8762
spring:
  application:
    name: SPRING-CLIENT-01
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka

DemoproviderApplication:

在这里插入图片描述


package com.huizhi.demoprovider1.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/getInfo")
public class UserController {

    @Value("${server.port}")
    String port;
    @Value("${spring.application.name}")
    String serviceName;
    @RequestMapping("/show")
    public String getInfo(){
        return "I'm form service:"+serviceName+",port:"+port;
    }
    @RequestMapping(value = "no/{no}", method = RequestMethod.GET)
    public String getPolicyFileByPolicyNo(@PathVariable String no,HttpServletResponse httpServletResponse){
        try {
            String  result = "I'm form service:"+serviceName+",port:"+port;
            if (result == null){
                httpServletResponse.setStatus(HttpServletResponse.SC_NOT_FOUND);
                return null;
            } else
                return result;

        } catch (Exception e) {
            e.printStackTrace();
            httpServletResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            return e.getMessage();
        }
    }

    @RequestMapping("/port")
    public String findInfo(){
        return "I'm form service:"+serviceName+",port:"+port;
    }
}
package com.huizhi.demoprovider1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaClient
@RestController

public class Demoprovider1Application {

    public static void main(String[] args) {
        SpringApplication.run(Demoprovider1Application.class, args);
    }
    /**
     * 测试方法
     */
    //动态取端口号,${server.port}和配置文件的值对应
    @Value("${server.port}")
    String post;
    @GetMapping("test")
    public String test(){
        //返回一句话
        return "I post port is :" +post;
    }
}

run,first-DemoserverApplication,second-DemoproviderApplication
在这里插入图片描述

发布了268 篇原创文章 · 获赞 47 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_39593940/article/details/103836097