使用SpringCloud搭建一個(gè)簡(jiǎn)單的項(xiàng)目

因?yàn)橐芯糠植际绞聞?wù) 所以搭建一個(gè)簡(jiǎn)單的SpringCloud的項(xiàng)目 來(lái)進(jìn)行事務(wù)相關(guān)的測(cè)試 中間出現(xiàn)了一些小問(wèn)題 在這里記錄一下創(chuàng)建項(xiàng)目的整個(gè)過(guò)程

  • 創(chuàng)建EurekServer
    創(chuàng)建一個(gè)springboot項(xiàng)目 并引入依賴web eurekserver hystrix-dashboard,'security'
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

修改配置文件 更改端口號(hào) 以及配置security

server:
  port: 8761

spring:
  application:
    name: registry
  security:
    user:
      name: zhou
      password: 12345678

eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
      defaultZone: http://zhou:12345678@localhost:${server.port}/eureka/

然后修改啟動(dòng)類(lèi)

@SpringBootApplication
@EnableEurekaServer
@EnableHystrixDashboard
public class RegistryApplication {

    public static void main(String[] args) {
        SpringApplication.run(RegistryApplication.class, args);
    }

}

然后建立一個(gè)zuul的網(wǎng)關(guān) 引入pom文件

   <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>

對(duì)yml文件進(jìn)行配置 修改端口號(hào) 增加application name

server:
  port: 8888

spring:
  application:
    name: proxy
eureka:
  client:
    serviceUrl:
      defaultZone:  http://zhou:12345678@localhost:8761/eureka

然后寫(xiě)一個(gè)user的項(xiàng)目來(lái)進(jìn)行測(cè)試 使用的是h2jpa
建立domain類(lèi)

@Entity(name = "customer")
public class Customer {
    @Id
    @GeneratedValue
    private Long userId;
    private String password;
    private String userName;

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}

建立CustomerRepositry 因?yàn)榫褪且粋€(gè)測(cè)試 所以只是繼承了JpaRepository

public interface CustomerRepositry extends JpaRepository<Customer,Long> {

}

service

@Service
public class CustomerService {
    @Resource
    private CustomerRepositry customerRepositry;

    //根據(jù)id 查詢用戶
    public Optional<Customer> getCustomerById(Long id){
        return customerRepositry.findById(id);
    }
    //查詢用戶信息
    public List<Customer> getAllCustomer(){
        return customerRepositry.findAll();
    }
    //保存用戶信息
    public void save(Customer customer){
        customerRepositry.save(customer);
    }
}

controller

@RestController
@RequestMapping("/user/test")
public class CustomerController {
    @Resource
    private CustomerService customerService;
    @Resource
    private OrderClient orderClient;

    @PostConstruct
    public void init(){
        Customer customer = new Customer();
        customer.setUserId(1L);
        customer.setUserName("KrisWu");
        customer.setPassword("123456");
        customerService.save(customer);
    }

    @RequestMapping("getCustomer")
    public List<Customer> getCustomer(){
        return customerService.getAllCustomer();
    }

    @RequestMapping("getOrder")
    @HystrixCommand
    public Map getOrder(){
        Optional<Customer> customer = customerService.getCustomerById(1L);
        String orderDetail = orderClient.getMyOrder(1L);
        Map map = new HashMap();
        map.put("customer",customer);
        map.put("orderDetail",orderDetail);
        return map;
    }

啟動(dòng)項(xiàng)目 出現(xiàn)了報(bào)錯(cuò)

ERROR 11612 --- [tbeatExecutor-0] com.netflix.discovery.DiscoveryClient    : *****:***** - was unable to send heartbeat!

這個(gè)錯(cuò)誤是在2.0以上會(huì)出現(xiàn)的。是因?yàn)閟ecurity默認(rèn)啟用了csrf檢驗(yàn),要在eurekaServer端配置security的csrf檢驗(yàn)為false求厕。
所以在repostory中新建一個(gè)config文件

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        super.configure(http);
    }
}

然后通過(guò)proxy的地址來(lái)訪問(wèn)user的項(xiàng)目
訪問(wèn)地址(http://localhost:8888/user/user/test/getCustomer)
然后發(fā)現(xiàn)可以顯示增加的用戶信息

通過(guò)zuul訪問(wèn)

然后再新建一個(gè)order的項(xiàng)目 想要達(dá)到的想過(guò)是可以在user的項(xiàng)目中 直接調(diào)用order中的方法 這里和user基本上是差不多的 就不貼代碼了
重要的地方是在user的項(xiàng)目中 新建一個(gè)類(lèi)OrderClient

@FeignClient(value = "order",path = "/order/test")
public interface OrderClient {
    @GetMapping("/{id}")
    String getMyOrder(@PathVariable(name = "id")Long id);
}

主要就是通過(guò)這個(gè)類(lèi)進(jìn)行連接
然后啟動(dòng)項(xiàng)目 進(jìn)行測(cè)試


image.png

image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市带污,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,406評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件恐锣,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡舞痰,警方通過(guò)查閱死者的電腦和手機(jī)土榴,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,395評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)响牛,“玉大人玷禽,你說(shuō)我怎么就攤上這事⊙酱颍” “怎么了矢赁?”我有些...
    開(kāi)封第一講書(shū)人閱讀 167,815評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)贬丛。 經(jīng)常有香客問(wèn)我撩银,道長(zhǎng),這世上最難降的妖魔是什么豺憔? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,537評(píng)論 1 296
  • 正文 為了忘掉前任额获,我火速辦了婚禮,結(jié)果婚禮上焕阿,老公的妹妹穿的比我還像新娘咪啡。我一直安慰自己,他們只是感情好暮屡,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,536評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布撤摸。 她就那樣靜靜地躺著,像睡著了一般褒纲。 火紅的嫁衣襯著肌膚如雪准夷。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 52,184評(píng)論 1 308
  • 那天莺掠,我揣著相機(jī)與錄音衫嵌,去河邊找鬼。 笑死彻秆,一個(gè)胖子當(dāng)著我的面吹牛楔绞,可吹牛的內(nèi)容都是我干的结闸。 我是一名探鬼主播,決...
    沈念sama閱讀 40,776評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼酒朵,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼桦锄!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起蔫耽,我...
    開(kāi)封第一講書(shū)人閱讀 39,668評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤结耀,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后匙铡,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體图甜,經(jīng)...
    沈念sama閱讀 46,212評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,299評(píng)論 3 340
  • 正文 我和宋清朗相戀三年鳖眼,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了黑毅。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,438評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡具帮,死狀恐怖博肋,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情蜂厅,我是刑警寧澤匪凡,帶...
    沈念sama閱讀 36,128評(píng)論 5 349
  • 正文 年R本政府宣布,位于F島的核電站掘猿,受9級(jí)特大地震影響病游,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜稠通,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,807評(píng)論 3 333
  • 文/蒙蒙 一衬衬、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧改橘,春花似錦滋尉、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,279評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至碌识,卻和暖如春碾篡,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背筏餐。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,395評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工开泽, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人魁瞪。 一個(gè)月前我還...
    沈念sama閱讀 48,827評(píng)論 3 376
  • 正文 我出身青樓穆律,卻偏偏與公主長(zhǎng)得像惠呼,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子峦耘,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,446評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容